const app = getApp()
var webhost = app.globalData.webhost;
import Toast from '../Vant/toast/toast';
import utils from '../../utils/date.js';
var listData;
var instance;

Page({
  data: {
    active: 0,
    dataList: [],
    show: false,
    actions: [],
    index: 0,
    page: 1,
    startDate: '',
    endDate: '',
    loading: 0
  },

  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    this.setData({ show: false });    
    switch (event.detail.name){
      case "异常登记":
        wx.navigateTo({
          url: '../func/doException?plan=1',
        })
        break;
      case "费用申请":
        wx.navigateTo({
          url: '../func/feeApplication?plan=1',
        })
        break;
      case "照片":
        wx.navigateTo({
          url: './img?lt=plan',
        })      
        break;
    }
  },

  onChange(event) {
    var that = this;
    that.setData({ active: event.detail.index });
    that.setData({
      page: 1,
      dataList: []
    })
    if (event.detail.index == 0){
      listData('PLAN0010');
    } else {
      listData('PLAN3040', that.data.startDate, that.data.endDate)
    }
  },

  setClick(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var taskCode = e.currentTarget.dataset.taskcode
    var actionCode = e.currentTarget.dataset.actioncode;
    instance(id, taskCode, actionCode);
  },

  moreClick(e){
    var that = this;
    var id = +e.currentTarget.dataset.id;
    that.setData({ index: id })
    var plan = that.data.dataList[id];
    app.globalData.plan = plan;
    app.globalData.task = {};
    app.globalData.taskid = '';
    var list = new Array();
    if (plan.abnormal){
      var a = '异常登记';
      // var a = { name: '异常登记' };
      list.push(a);
    }
    if (plan.cost) {
      // var b = { name: "费用申请" };
      var b = "费用申请";
      list.push(b);
    }
    if (plan.snap) {
      var b = "照片";
      // var b = { name: "照片" };
      list.push(b);
    }
    that.setData({ actions: list });    
    // that.setData({ show: true });
    wx.showActionSheet({
      itemList: that.data.actions,
      success(res) {
        var item = that.data.actions[res.tapIndex];
        switch (item) {
          case "异常登记":
            wx.navigateTo({
              url: '../func/doException?plan=1',
            })
            break;
          case "费用申请":
            wx.navigateTo({
              url: '../func/feeApplication?plan=1',
            })
            break;
          case "照片":
            wx.navigateTo({
              url: './img?lt=plan',
            })
            break;
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })

  },

  viewClick(e) {
    var id = +e.currentTarget.dataset.id;
    var plan = this.data.dataList[id];
    app.globalData.planid = plan.planId;
    app.globalData.plan = plan;
    wx.navigateTo({
      url: './tdetail'
    })
  },

  bindDate: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      page: 1,
      dataList: [],
      startDate: date
    })
    listData('PLAN3040', that.data.startDate, that.data.endDate);
  },

  bindDate2: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      page: 1,
      dataList: [],
      endDate: date
    })
    listData('PLAN3040', that.data.startDate, that.data.endDate);
  },

  onLoad: function() {
    var that = this;
    var myDate = new Date();
    var myDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);
    var date = utils.Format('yyyy-MM-dd', myDate);
    var date2 = utils.Format('yyyy-MM-dd', myDate2);
    that.setData({
      startDate: date2,
      endDate: date,
      date: date
    })

    listData = function( state ,start, end) {
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          stateCode: state,          
          dateStart: start,
          dateEnd: end,
          pageNumber: that.data.page,
          pageSize: 5
        }
      }]
      console.log(data)
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'queryPlan', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({data}) {
          if (data.code && data.code == '000000') {
            var list = data.dataList;
            if(list.length > 0){
              console.log('有数据')
              var newList = new Array();
              for (var i = 0; i < list.length; i++) {
                var item = list[i];
                if(item.actionList){
                  for (var j = 0; j < item.actionList.length; j++) {
                    switch (item.actionList[j].actionCode) {
                      case 'AC10':
                        item.start = item.actionList[j];
                        break;
                      case 'AC30':
                        item.end = item.actionList[j];
                        break;
                      case 'AC40':
                        item.stop = item.actionList[j];
                        break;
                      case 'EXCE':
                        item.abnormal = item.actionList[j];
                        break;
                      case 'COST':
                        item.cost = item.actionList[j];
                        break;
                      case 'SNAP':
                        item.snap = item.actionList[j];
                        break;
                    }
                  }
                }
                newList.push(item);
              }
              that.setData({
                loading: 0,               
                dataList: that.data.dataList.concat(newList)
              })           
            } else {
              console.log('无数据')              
              that.setData({ loading: 2})
            }
          } else if (data.code == '003000') {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            setTimeout(function(){
              wx.redirectTo({
                url: '../login/login',
              })
            }, 1200);
          }
        },
        fail: function(res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

    instance = function(id, taskCode, actionCode){
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          planId: id,
          taskid: '',
          taskCode: taskCode,
          actionCode: actionCode,
          remarks: ''
        }
      }]
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'doGeneralEvent', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({ data }) {
          console.log(data)
          if (data.code && data.code == '000000') {
            wx.showToast({
              title: '操作成功',
              icon: 'success'
            })
            that.setData({
              dataList: [],
              page: 1
            })
            if(that.data.active == 0){
              listData('PLAN0010');
            } else {
              listData('PLAN3040', that.data.startDate, that.data.endDate)
            }
          } else {
            wx.showModal({
              title: '操作异常',
              showCancel: false,
              confirmColor: '#118EDE',
              content: data.msg,
              success:function(res){
                if (data.code && data.code == '003000') {
                  if (res.confirm) {
                    wx.reLaunch({
                      url: '../login/login'
                    })
                  }
                }
              }
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

    listData('PLAN0010');

   
  },

  onPullDownRefresh() {
    var that = this;
    setTimeout(function() {
      that.setData({
        page: 1,
        dataList: []
      })
      if (that.data.active == 0) {
        listData('PLAN0010');
      } else {
        listData('PLAN3040', that.data.startDate, that.data.endDate)
      }
      wx.stopPullDownRefresh()
    }, 2000)
  },

  onShareAppMessage() {

  },

  onShow(){
    var that = this;
    setTimeout(function () {
      that.setData({
        page: 1,
        dataList: []
      })
      if (that.data.active == 0) {
        listData('PLAN0010');
      } else {
        listData('PLAN3040', that.data.startDate, that.data.endDate)
      }
    }, 500)
  },

  onReachBottom(){
    var that = this;
    console.log(that.data.loading);
    if(that.data.loading == 0){
      that.setData({
        loading: true,
        page: that.data.page + 1
      })
      setTimeout(() => {
        if (that.data.active == 0) {
          listData('PLAN0010');
        } else {
          listData('PLAN3040', that.data.startDate, that.data.endDate)
        }
      }, 300);
    }
  }
})